package org.dromara.curd.dnnyun.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.curd.dnnyun.domain.DnnyunStoreAchievement;
import org.dromara.curd.dnnyun.domain.bo.DnnyunStoreAchievementBo;
import org.dromara.curd.dnnyun.mapper.DnnyunStoreAchievementMapper;
import org.springframework.stereotype.Service;
import org.dromara.curd.dnnyun.domain.vo.DnnyunStoreAchievementVo;
import org.dromara.curd.dnnyun.service.IDnnyunStoreAchievementService;

import java.util.*;

/**
 * 店面业绩统计Service业务层处理
 *
 * @author wdm
 * @date 2024-03-07
 */
@RequiredArgsConstructor
@Service
public class DnnyunStoreAchievementServiceImpl implements IDnnyunStoreAchievementService {

    private final DnnyunStoreAchievementMapper baseMapper;

    /**
     * 查询店面业绩统计
     */
    @Override
    public DnnyunStoreAchievementVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询店面业绩统计列表
     */
    @Override
    public TableDataInfo<DnnyunStoreAchievementVo> queryPageList(DnnyunStoreAchievementBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DnnyunStoreAchievement> lqw = buildQueryWrapper(bo);
        Page<DnnyunStoreAchievementVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询店面业绩统计列表
     */
    @Override
    public List<DnnyunStoreAchievementVo> queryList(DnnyunStoreAchievementBo bo) {
        LambdaQueryWrapper<DnnyunStoreAchievement> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DnnyunStoreAchievement> buildQueryWrapper(DnnyunStoreAchievementBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DnnyunStoreAchievement> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStoreId() != null, DnnyunStoreAchievement::getStoreId, bo.getStoreId());
        lqw.eq(bo.getOrderDate() !=null, DnnyunStoreAchievement::getOrderDate, bo.getOrderDate());
        lqw.between(params.get("beginOrderDate") != null && params.get("endOrderDate") != null,
            DnnyunStoreAchievement::getOrderDate ,params.get("beginOrderDate"), params.get("endOrderDate"));
        return lqw;
    }

    /**
     * 新增店面业绩统计
     */
    @Override
    public Boolean insertByBo(DnnyunStoreAchievementBo bo) {
        DnnyunStoreAchievement add = MapstructUtils.convert(bo, DnnyunStoreAchievement.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean insertBatchByBo(List<DnnyunStoreAchievementBo> boList) {
        if(CollectionUtil.isNotEmpty(boList)){
            List<DnnyunStoreAchievement> batchInsertList = new ArrayList<>();
            for (DnnyunStoreAchievementBo bo:boList) {
                DnnyunStoreAchievement add = MapstructUtils.convert(bo, DnnyunStoreAchievement.class);
                validEntityBeforeSave(add);
                batchInsertList.add(add);
            }
            return baseMapper.insertBatch(batchInsertList);
        }
        return true;
    }

    /**
     * 修改店面业绩统计
     */
    @Override
    public Boolean updateByBo(DnnyunStoreAchievementBo bo) {
        DnnyunStoreAchievement update = MapstructUtils.convert(bo, DnnyunStoreAchievement.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DnnyunStoreAchievement entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除店面业绩统计
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean deleteByOrderDate(Date orderDate) {
        DnnyunStoreAchievementBo bo = new DnnyunStoreAchievementBo();
        bo.setOrderDate(orderDate);
        LambdaQueryWrapper<DnnyunStoreAchievement> lqw = buildQueryWrapper(bo);
        return baseMapper.delete(lqw) > 0;
    }
}
